﻿using System;
using Csla;
using BizControl.Library.BusinessObjects.Activities;
using BizControl.DalLinq;
using BizControl.Resources;
using System.Linq;
using System.Collections.Generic;

namespace BizControl.Library.BusinessObjects.Marks
{
  [Serializable()]
  public class MarkConditions : BusinessListBase<MarkConditions, MarkConditionBO>
  {
    #region  Business Methods

    private MarkInfo _mark;

    public MarkConditionBO AddMarkCondition(ActivityInfo requiredActivity, MarkInfo requiredMark, ConditionType type)
    {
      MarkConditionBO markCondition = MarkConditionBO.NewMarkCondition(_mark, requiredActivity, requiredMark, type);
      this.Add(markCondition);
      return markCondition;
    }

    public MarkConditionBO AddMarkCondition(ConditionType type)
    {
      MarkConditionBO markCondition = MarkConditionBO.NewMarkCondition(_mark, type);
      this.Add(markCondition);
      return markCondition;
    }

    public void Remove(Guid conditionId)
    {
      foreach (MarkConditionBO condition in this)
      {
        if (condition.MarkConditionId == conditionId)
        {
          Remove(condition);
          break;
        }
      }
    }

    #endregion

    #region  Factory Methods

    internal static MarkConditions NewMarkConditions(MarkInfo mark)
    {
      return DataPortal.CreateChild<MarkConditions>(mark);
    }

    internal static MarkConditions GetMarkConditions(MarkCondition[] data, MarkInfo mark)
    {
      return DataPortal.FetchChild<MarkConditions>(data, mark);
    }

    private MarkConditions()
    { /* require use of factory methods */ }

    #endregion

    #region  Data Access

    private void Child_Create(MarkInfo mark)
    {
      if (null == mark) throw new ArgumentNullException(ExceptionsMessages.MarkNullReference);
      _mark = mark;
    }

    private void Child_Fetch(MarkCondition[] data, MarkInfo mark)
    {
      if (null == mark) throw new ArgumentNullException(ExceptionsMessages.MarkNullReference);
      _mark = mark;
      if (data == null) return;
      this.RaiseListChangedEvents = false;
      foreach (var child in data)
        Add(MarkConditionBO.GetMarkCondition(child));
      this.RaiseListChangedEvents = true;
    }

    #endregion
  }
}
